<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Person extends Site {

    function setup() {
        $this->run_modes('report', 'attention', 'recommen', 'blog', 'ajax', 'clear');
        $this->start_mode('report');
        $this->selfurl = '/person.php';
        $this->pagesize = 3;
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun($rm = '') {
        $this->ProductReports = getModel('Model_ProductReports');
        $this->member = $this->s_param('memberData');
        $this->Points = getModel('Model_Points');
        $this->Person = getModel('Model_Person');
        $this->ProductReportsComment = getModel('Model_ProductReportsComment');
        require_once(LIB_PATH . '/Common/HTMLPurifier/HTMLPurifier.standalone.php');
        parent::cgiapp_prerun();
    }

    function action_report() {
        $uid = intval($this->get_q('uid', 0, 'int'));
        $page = intval($this->get_q('page', 1, 'int'));
        $username = $this->get_q('username', '');
        $t = intval($this->get_q('t', 0, 'int'));

        $kvcobj1 = getCache('person_get', array('expire' => KVC_EXPIRE));
        $user = $kvcobj1->get($this->member['id'] . '_' . $uid . '_' . $username);
        if (!$user) {
            $user = $this->Person->get_user($this->member['id'], $uid, $username);
            $kvcobj1->set($this->member['id'] . '_' . $uid . '_' . $username, $user);
        }

        $uid = empty($uid) ? $user['result'][0]['id'] : $uid;
        $username = empty($username) ? $user['result'][0]['username'] : $username;

        $kvcobj = getCache('person_report', array('expire' => KVC_EXPIRE));

        $user_reports = array();
        $user_reports_res = $kvcobj->get($uid . '_'.$t .'_'. $page . '_user_reports_res');
        if (!$user_reports_res) {
            $user_reports_res = $this->ProductReports->getReportsList(0, $uid, 0, "TB1.addtime desc", $page, $this->pagesize, 1, '', 0, 0, $t);
            $kvcobj->set($uid . '_'.$t .'_'. $page . '_user_reports_res', $user_reports_res);
        }
        $user_reports = $kvcobj->get($uid . '_'.$t .'_' . $page . '_user_reports');
        if (!$user_reports) {
            $user_reports = $this->Person->get_user_report($user_reports_res, $username);
            if ($user_reports) {
                $global_url = $this->param('URL');
                foreach ($user_reports as $key => $value) {

                    $fb_result = file_get_contents("http://graph.facebook.com/" . $global_url . substr($value['report']['share_url'], 1));
                    $fb_array = json_decode($fb_result, 1);

                    $value['report']['fb_count'] = $fb_array['shares'] + $value['report']['fb_share_num_base'];
                    $user_reports[$key]['report'] = $value['report'];
                }
            }
            $kvcobj->set($uid . '_'.$t .'_' . $page . '_user_reports', $user_reports);
        }
        $t_arr = array(0 => "all", 1 => "blog", 2 => "report");
        $pagelist = showPage($user_reports_res['pageinfo']['total'], $this->pagesize, $page, optimize_url('person', 'report', 'username=' . $username) . '/' . $t_arr[$t]);

        //echo '<pre>'.var_export($user['result'][0],1).'</pre>';

        $mid = $this->member['id'];
        $meta = $this->meta;
        if (!empty($meta)) {
            foreach ($meta as $k => $v) {
                $meta[$k] = str_replace('[person]', $user['result'][0]['username'], $v);
            }
        }
        $select = array(0 => array("title" => "全部", "name" => "all"), 2 => array("title" => "報告", "name" => "report"), 1 => array("title" => "Blog", "name" => "blog"));
        $pageoptions = array();
        $pageoptions['csses'][0] = "jquery.lightbox-0.5";
        $pageoptions['jses'][0] = "jquery.lightbox-0.5.min";
        $this->tmpl_assign('select', $select);
        $this->tmpl_assign('t', $t);
        $this->tmpl_assign('pageoptions', $pageoptions);
        $this->tmpl_assign('meta', $meta);
        $this->tmpl_assign('mid', $mid);
        $this->tmpl_assign('uid', $uid);
        $this->tmpl_assign('username', $username);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('user', $user['result'][0]);
        $this->tmpl_assign('user_reports', $user_reports);
        return $this->load_tmpl("per_report.php");
    }

    function action_blog() {
        $uid = intval($this->get_q('uid', 0, 'int'));
        $username = $this->get_q('username', '');
        $bid = intval($this->get_q('bid', 0, 'int'));
        $page = intval($this->get_q('page', 1, 'int'));
        $pre = intval($this->get_q('p', 0, 'int'));
        if (empty($bid) && empty($pre)) {
            showMsgCode("id無效", '/');
        }

        $kvcobj1 = getCache('person_get', array('expire' => KVC_EXPIRE));
        $user = $kvcobj1->get($this->member['id'] . '_' . $uid . '_' . $username);
        if (!$user) {
            $user = $this->Person->get_user($this->member['id'], $uid, $username);
            $kvcobj1->set($this->member['id'] . '_' . $uid . '_' . $username, $user);
        }
        $uid = empty($uid) ? $user['result'][0]['id'] : $uid;
        $username = empty($username) ? $user['result'][0]['username'] : $username;

        $user_reports = $reportsCommentList = array();
        $pagelist = '';
        if (!empty($pre)) {
            $config = HTMLPurifier_Config::createDefault();
            $config->set('HTML.TargetBlank', true);
            $config->set('HTML.Allowed', 'span[style],em,strong,img[src|align|height|width|alt],br,b,p,div[style]');
            $HTMLPurifierObj = new HTMLPurifier($config);
            $user_reports[0]['report']['body'] = $HTMLPurifierObj->purify($this->get_q('content', ''));
            $user_reports[0]['report']['title'] = $this->get_q('title', '');
            $user_reports[0]['report']['agegroup'] = intval($this->get_q('age', 1, 'int'));
            $user_reports[0]['report']['body_skin'] = intval($this->get_q('skin', 1, 'int'));
            $user_reports[0]['report']['well'] = intval($this->get_q('well', 0, 'int'));

            $vote_option_data = array('result' => array('滿意度', '抵買度', '舒適度', '清爽度', '持久度', '外型'));
            foreach ($vote_option_data['result'] as $k1 => $v1) {
                $_category = intval($this->get_q('category_score' . ($k1 + 1), 0, 'int'));
                if(!empty($_category)){
                    $user_reports[0]['report']['category'][$k1]['k'] = $k1;
                    $user_reports[0]['report']['category'][$k1]['name'] = $v1;
                    $user_reports[0]['report']['category'][$k1]['value'] = $_category;
                }
            }
        } else {
            $kvcobj = getCache('person_blog', array('expire' => KVC_EXPIRE));
            $user_reports_res = $kvcobj->get($uid . '_' . $bid . '_user_reports_res');
            if (!$user_reports_res) {
                $user_reports_res = $this->ProductReports->getReportsList(0, $uid, $bid, "TB1.id desc", 1, 1, 1, '', 0, 0, 0);
                $kvcobj->set($uid . '_' . $bid . '_user_reports_res', $user_reports_res);
            }
            $user_reports = $kvcobj->get($uid . '_' . $bid . '_user_reports');
            if (!$user_reports) {
                $user_reports = $this->Person->get_user_report($user_reports_res, $username);
                if ($user_reports) {
                    $global_url = $this->param('URL');
                    foreach ($user_reports as $key => $value) {
                        $fb_result = file_get_contents("http://graph.facebook.com/" . $global_url . substr($value['report']['share_url'], 1));
                        $fb_array = json_decode($fb_result, 1);
                        $value['report']['fb_count'] = $fb_array['shares'] + $value['report']['fb_share_num_base'];
                        $user_reports[$key]['report'] = $value['report'];
                    }
                }
                $kvcobj->set($uid . '_' . $bid . '_user_reports', $user_reports);
            }

            $orderby = "TB1.addtime desc,TB1.id desc";
            $pagesize = 5;
            $reportsCommentList = $this->ProductReportsComment->getReportsCommentList($bid, $orderby, $page, $pagesize);
            if (!empty($reportsCommentList['result'])) {
                foreach ($reportsCommentList['result'] as $k => $v) {
                    $reportsCommentList['result'][$k]['photo_l'] = imore_avatar($v['photo']);
                    $reportsCommentList['result'][$k]['addtime'] = time_to_str($v['addtime']);
                }
            }
            $pagelist = showPage($reportsCommentList['pageinfo']['total'], $pagesize, $page, optimize_url('person', 'blog', 'username=' . $username . '&bid=' . $bid), 0, '#messagebox');
        }

        $mid = $this->member['id'];
        $meta = $this->meta;
        if (!empty($meta)) {
            foreach ($meta as $k => $v) {
                $meta[$k] = str_replace('[person]', $user['result'][0]['username'], $v);
            }
        }
        $photo = empty($this->member) ? PhotoResize('', 58, 58) : imore_avatar($this->member['photo']);
        $pageoptions = array();
        $pageoptions['csses'][0] = "jquery.lightbox-0.5";
        $pageoptions['jses'][0] = "jquery.lightbox-0.5.min";
        $reply_type = 0; //  0 -itrial 自己的留言下，自己的回覆可移除，別人的可舉報   1 -gotrip 自己的留言下，所有回覆可移除
        //echo "<pre>".var_export($user_reports[0],1)."</pre>";

        $this->tmpl_assign('pre', $pre);
        $this->tmpl_assign('photo', $photo);
        $this->tmpl_assign('reportsCommentList', $reportsCommentList);
        $this->tmpl_assign('reply_type', $reply_type);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('pageoptions', $pageoptions);
        $this->tmpl_assign('meta', $meta);
        $this->tmpl_assign('mid', $mid);
        $this->tmpl_assign('uid', $uid);
        $this->tmpl_assign('username', $username);
        $this->tmpl_assign('user', $user['result'][0]);
        $this->tmpl_assign('user_reports', $user_reports[0]);

        return $this->load_tmpl("per_blog.php");
    }

    function action_attention() {
        $uid = intval($this->get_q('uid', 0, 'int'));
        $page = intval($this->get_q('page', 1, 'int'));
        $t = intval($this->get_q('t', 0, 'int')); //區分關注與粉絲
        $ps = intval($this->get_q('ps', 3, 'int')); //pagesize
        $username = $this->get_q('username', '');

        $kvcobj1 = getCache('person_get', array('expire' => KVC_EXPIRE));
        $user = $kvcobj1->get($this->member['id'] . '_' . $uid . '_' . $username);
        if (!$user) {
            $user = $this->Person->get_user($this->member['id'], $uid, $username);
            $kvcobj1->set($this->member['id'] . '_' . $uid . '_' . $username, $user);
        }

        $uid = empty($uid) ? $user['result'][0]['id'] : $uid;
        $username = empty($username) ? $user['result'][0]['username'] : $username;

        $kvcobj = getCache('person_attention', array('expire' => 3));

        $userlikes = $kvcobj->get($uid . '_' . $page . '_' . $ps . '_userlikes');
        if (!$userlikes) {
            $userlikes = $this->Person->get_userlike($uid, $page, $ps);
            $kvcobj->set($uid . '_' . $page . '_' . $ps . '_userlikes', $userlikes);
        }

        $likeusers = $kvcobj->get($uid . '_' . $page . '_' . $ps . '_likeusers');
        if (!$likeusers) {
            $likeusers = $this->Person->get_likeuser($uid, $page, $ps);
            $kvcobj->set($uid . '_' . $page . '_' . $ps . '_likeusers', $likeusers);
        }

        $attent_count1 = $kvcobj->get($uid . '_attent_count1');
        if (!$attent_count1) {
            $attent_count1 = db_query_count('member_like', "section='member' and mid=" . mysql_escape_string($uid));
            $kvcobj->set($uid . '_attent_count1', $attent_count1);
        }

        $attent_count2 = $kvcobj->get($uid . '_attent_count2');
        if (!$attent_count2) {
            $attent_count2 = db_query_count('member_like', "section='member' and sid=" . mysql_escape_string($uid));
            $kvcobj->set($uid . '_attent_count2', $attent_count2);
        }

        $pagelist1 = showPage($attent_count1, $ps, $page, optimize_url('person', 'attention', 'username=' . $username . '&t=' . $t . '&ps=6'));
        $pagelist2 = showPage($attent_count2, $ps, $page, optimize_url('person', 'attention', 'username=' . $username . '&t=' . $t . '&ps=6'));

        $mid = $this->member['id'];
        $meta = $this->meta;
        if (!empty($meta)) {
            foreach ($meta as $k => $v) {
                $meta[$k] = str_replace('[person]', $user['result'][0]['username'], $v);
            }
        }
        $this->tmpl_assign('meta', $meta);

        $this->tmpl_assign('t', $t);
        $this->tmpl_assign('mid', $mid);
        $this->tmpl_assign('uid', $uid);
        $this->tmpl_assign('username', $username);
        $this->tmpl_assign('pagelist1', $pagelist1);
        $this->tmpl_assign('pagelist2', $pagelist2);
        $this->tmpl_assign('attent_count1', $attent_count1);
        $this->tmpl_assign('attent_count2', $attent_count2);
        $this->tmpl_assign('user', $user['result'][0]);
        $this->tmpl_assign('userlikes', $userlikes);
        $this->tmpl_assign('likeusers', $likeusers);
        return $this->load_tmpl("per_attention.php");
    }

    function action_recommen() {
        $uid = intval($this->get_q('uid', 0, 'int'));
        $page = intval($this->get_q('page', 1, 'int'));
        $username = $this->get_q('username', '');

        $kvcobj1 = getCache('person_get', array('expire' => KVC_EXPIRE));
        $user = $kvcobj1->get($this->member['id'] . '_' . $uid . '_' . $username);
        if (!$user) {
            $user = $this->Person->get_user($this->member['id'], $uid, $username);
            $kvcobj1->set($this->member['id'] . '_' . $uid . '_' . $username, $user);
        }

        $uid = empty($uid) ? $user['result'][0]['id'] : $uid;
        $username = empty($username) ? $user['result'][0]['username'] : $username;
        $kvcobj = getCache('person_recommen', array('expire' => KVC_EXPIRE));
        $user_like_reports = $kvcobj->get($uid . '_' . $page . '_user_like_reports');
        if (!$user_like_reports) {
            $user_like_reports = $this->Person->get_user_like_report($uid, $page, $this->pagesize);
            if ($user_like_reports) {
                $global_url = $this->param('URL');
                foreach ($user_like_reports as $key => $value) {

                    $fb_result = file_get_contents("http://graph.facebook.com/" . $global_url . substr($value['report']['share_url'], 1));
                    $fb_array = json_decode($fb_result, 1);

                    $value['report']['fb_count'] = $fb_array['shares'] + $value['report']['fb_share_num_base'];
                    $user_like_reports[$key]['report'] = $value['report'];
                }
            }
            $kvcobj->set($uid . '_' . $page . '_user_like_reports', $user_like_reports);
        }
        $recommen_count = $kvcobj->get($uid . '_recommen_count');
        if (!$recommen_count) {
            $recommen_count = db_query_count('member_like', "section='report' and mid=" . mysql_escape_string($uid));
            $kvcobj->set($uid . '_recommen_count', $recommen_count);
        }
        $pagelist = showPage($recommen_count, $this->pagesize, $page, optimize_url('person', 'recommen', 'username=' . $username));

        $mid = $this->member['id'];
        $meta = $this->meta;
        if (!empty($meta)) {
            foreach ($meta as $k => $v) {
                $meta[$k] = str_replace('[person]', $user['result'][0]['username'], $v);
            }
        }
        $this->tmpl_assign('meta', $meta);
        $this->tmpl_assign('mid', $mid);
        $this->tmpl_assign('uid', $uid);
        $this->tmpl_assign('username', $username);
        $this->tmpl_assign('pagelist', $pagelist);
        $this->tmpl_assign('user', $user['result'][0]);
        $this->tmpl_assign('user_like_reports', $user_like_reports);
        return $this->load_tmpl("per_recommen.php");
    }

    function action_ajax() {
        $t = intval($this->get_q('t', 1, 'int'));
        switch ($t) {
            case 1://關注會員
                if (empty($this->member)) {
                    echo json_encode(array('recode' => 1, 'msg' => NO_LOGIN));
                    exit;
                }

                $uid = intval($this->get_q('uid', 0, 'int'));
                if ($uid == $this->member['id']) {
                    echo json_encode(array('recode' => 3, 'msg' => NOT_ATTENTION_SELF));
                    exit;
                }
                if (!empty($uid)) {
                    $attr = array();
                    $attr['section'] = 'member';
                    $attr['mid'] = $this->member['id'];
                    $attr['sid'] = $uid;
                    $attr['addtime'] = date('Y-m-d H:i:s');
                    $attr['status'] = 1;
                    $is_n = db_query_count('member_like', "section='member' and mid=" . mysql_escape_string($this->member['id']) . " and sid=" . mysql_escape_string($uid));
                    $res = 0;
                    if ($is_n > 0) {
                        $res = db_update('member_like', array('status' => 1), "section='member' and mid=" . mysql_escape_string($this->member['id']) . " and sid=" . mysql_escape_string($uid));
                    } else {
                        $res = db_add('member_like', $attr);
                        if ($res)
                            $user = $this->Person->get_user($this->member['id'], $uid, '');
                        $this->Points->points_record($this->member['id'], "follow_user", str_replace("[會員名稱]", $user['result'][0]['username'], FOLLOW_USER_LANG));
                    }
                    if ($res) {
                        echo json_encode(array('recode' => 2, 'msg' => 'success'));
                        exit;
                    }
                }
                echo json_encode(array('recode' => 3, 'msg' => 'error'));
                exit;
                break;
            case 2://取消關注會員
                if (empty($this->member)) {
                    echo json_encode(array('recode' => 1, 'msg' => NO_LOGIN));
                    exit;
                }
                $uid = intval($this->get_q('uid', 0, 'int'));
                if (!empty($uid)) {
                    $attr = array();
                    $attr['section'] = 'member';
                    $attr['mid'] = $this->member['id'];
                    $attr['sid'] = $uid;
                    $attr['status'] = 0;
                    $res = db_update('member_like', array('status' => 0), "section='member' and mid=" . mysql_escape_string($this->member['id']) . " and sid=" . mysql_escape_string($uid));
                    if ($res) {
                        echo json_encode(array('recode' => 2, 'msg' => 'success'));
                        exit;
                    }
                }
                echo json_encode(array('recode' => 3, 'msg' => 'error'));
                exit;
                break;
            default:
                break;
        }
    }

    function action_clear() {
        $kvc = trim($this->get_q('kvc', ''));
        clear_kvcache($kvc);
    }

}

$webapp = new Person();
$webapp->run();
?>